package com.lin.lazybone;

import android.content.Context;
import android.widget.Toast;

import com.lin.lazybone.entity.Message;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.fragment.MesFragment;
import com.lin.lazybone.utils.L;

import cn.bmob.newim.event.MessageEvent;
import cn.bmob.newim.event.OfflineMessageEvent;
import cn.bmob.newim.listener.BmobIMMessageHandler;
import cn.bmob.v3.BmobUser;

/**
 * Created by mr.lin on 2017/11/27.
 */

public class ImMessageHandler extends BmobIMMessageHandler
{
    private Context context;


    public ImMessageHandler(Context context)
    {
        this.context = context;
    }

    @Override
    public void onMessageReceive(MessageEvent messageEvent)
    {
        super.onMessageReceive(messageEvent);
        //在线消息
        String name = messageEvent.getFromUserInfo().getUserId();
        boolean temp = false;
//        Toast.makeText(context, "用户" + name + "发来新的消息" + messageEvent.getMessage().getContent(), Toast.LENGTH_SHORT).show();
        for (Message m : MesFragment.messageList)
        {
            if (m.getYourname().equals(name))
            {
                temp = true;
                break;
            }
        }
        if (!temp)
        {
            Message m = new Message();
            m.setRecentMes(messageEvent.getMessage().getContent());
            _User user = BmobUser.getCurrentUser(_User.class);
            m.setMyname(user.getUsername());
            m.setYourname(name);
            MesFragment.messageList.add(m);
        }
    }

    @Override
    public void onOfflineReceive(OfflineMessageEvent offlineMessageEvent)
    {
        super.onOfflineReceive(offlineMessageEvent);
    }
}
